package com.leiyuee.mybatis.service.impl;


import com.leiyuee.mybatis.service.TKMapperService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * tk mapper 通用service
 * @param <T>
 * @param <ID>
 */

public abstract class TkMapperServiceImpl<T, ID > implements TKMapperService<T, ID> {
    @Override
    public abstract Mapper<T> getMapper();


    @Transactional
    @Override
    public int insert(T record) {
        return getMapper().insert(record);
    }
    @Transactional
    @Override
    public int insertSelective(T record) {
        return getMapper().insertSelective(record);
    }
    @Transactional
    @Override
	@Caching(evict= {
		@CacheEvict(value="serviceCache",key=" targetClass + 'selectByPK' + #p0.getId()")
	})
    public int updateByPK(T record) {
        return getMapper().updateByPrimaryKey(record);
    }
    @Transactional
    @Override
	@Caching(evict= {
		@CacheEvict(value="serviceCache",key=" targetClass + 'selectByPK' + #p0.getId()")
	})
    public int updateByPKSelective(T record) {
        return getMapper().updateByPrimaryKeySelective(record);
    }
    @Transactional
    @Override
	@Caching(evict= {
		@CacheEvict(value="serviceCache",key=" targetClass + 'selectByPK' + #p0",condition="#p0 != null")
	})
    public int deletByPK(ID id) {
        return getMapper().deleteByPrimaryKey(id);
    }
    @Override
    @Caching(cacheable= {
		@Cacheable(value="serviceCache",key=" targetClass+'.'+ methodName +'.' + #p0",unless="#result == null")
	})
    public T selectByPK(ID id) {
        return getMapper().selectByPrimaryKey(id);
    }
    @Override
    public T selectOne(T record) {
        return getMapper().selectOne(record);
    }
    @Override
    public List<T> select(T record) {
        return getMapper().select(record);
    }
    @Override
    public List<T> selectAll() {
        return getMapper().selectAll();
    }
    @Override
    public List<T> selectByExample(Example example){
        return getMapper().selectByExample(example);
    }
/*
    @Transactional
    @Override
    public int insert(T record) {
        return getMapper().insert(record);
    }
    @Transactional
    @Override
    public int insertSelective(T record) {
        return getMapper().insertSelective(record);
    }
    @Transactional
    @Override
    public int updateByPK(T record) {
        return getMapper().updateByPrimaryKey(record);
    }
    @Transactional
    @Override
    public int updateByPKSelective(T record) {
        return getMapper().updateByPrimaryKeySelective(record);
    }
    @Transactional
    @Override
    public int deletByPK(ID id) {
        return getMapper().deleteByPrimaryKey(id);
    }
    @Override
    public T selectByPK(ID id) {
        return getMapper().selectByPrimaryKey(id);
    }
    @Override
    public T selectOne(T record) {
        return getMapper().selectOne(record);
    }
    @Override
    public List<T> select(T record) {
        return getMapper().select(record);
    }
    @Override
    public List<T> selectAll() {
        return getMapper().selectAll();
    }
    @Override
    public List<T> selectByExample(Example example){
        return getMapper().selectByExample(example);
    }
*/


/*  --通过获取sqlSession 执行sql BaseService中的功能

    @Autowired
    private SqlSession sqlSession;

    private SqlMapper sqlMapper;

    public SqlSession getSqlSession() {
        if(sqlSession==null)
            sqlSession = (SqlSession) SpringContextHolder.getBean("sqlSession");
        return sqlSession;
    }

    public SqlMapper getSqlMapper() {
        if(sqlMapper==null)
            sqlMapper = new SqlMapper(getSqlSession());
        return sqlMapper;
    }


    @Override
    public Map<String, Object> selectOne(String sql) {
        return getSqlMapper().selectOne(sql);
    }

    @Override
    public Map<String, Object> selectOne(String sql, Object value) {
        return getSqlMapper().selectOne(sql,value);
    }


    @Override
    public <T> T selectOne(String sql, Class<T> resultType) {
        return getSqlMapper().selectOne(sql,resultType);
    }


    @Override
    public <T> T selectOne(String sql, Object value, Class<T> resultType) {
        return getSqlMapper().selectOne(sql,value,resultType);
    }

    @Override
    public List<Map<String, Object>> selectList(String sql) {
        return getSqlMapper().selectList(sql);
    }

    @Override
    public List<Map<String, Object>> selectList(String sql, Object value) {
        return getSqlMapper().selectList(sql,value);
    }

    @Override
    public <T> List<T> selectList(String sql, Class<T> resultType) {
        return getSqlMapper().selectList(sql,resultType);
    }

    @Override
    public <T> List<T> selectList(String sql, Object value, Class<T> resultType) {
        return getSqlMapper().selectList(sql,value,resultType);
    }

    @Override
    public int insert(String sql) {
        return getSqlMapper().insert(sql);
    }

    @Override
    public int update(String sql) {
        return getSqlMapper().update(sql);
    }
*/

}
